package com.tust;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class App {

    private static ArrayList<User> userList = new ArrayList<>();

    static {
        userList.add(new User("admin","123456","110101100902010011","12345678910"));
    }


    public static void main(String[] args) {
        String menu;
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择操作1登录 2注册 3忘记密码 4退出");

            menu = scanner.next();

            switch (menu) {
                case "1" -> login(userList);
                case "2" -> register(userList);
                case "3" -> forgotPassword(userList);
                case "4" -> {
                    System.out.println("谢谢使用，再见！");
                    //停止虚拟机运行
                    System.exit(0);
                }
                default -> System.out.println("查无此项，请重新输入！");
            }
        }
    }

    private static void login(ArrayList<User> userList) {

        for (int i = 0; i < 3; i++) {
            Scanner scanner = new Scanner(System.in);

            System.out.println("请输入用户名：");

            String username = scanner.next();

            if (!contains(userList, username)) {
                System.out.println("用户名" + username + "未注册，请先注册再登录！");
                return;
            }

            System.out.println("请输入密码：");
            String password = scanner.next();

            while (true) {
                String rightCode = getCode();
                System.out.println("当前验证码为：" + rightCode);

                System.out.println("请输入验证码：");

                String code = scanner.next();

                if (code.equalsIgnoreCase(rightCode)) {
                    break;
                }

                System.out.println("验证码错误，请重新输入！");
            }

            // 验证用户名和密码是否正确
            User userInfo = new User(username, password, null, null);

            if (checkUserInfo(userList, userInfo)) {
                System.out.println("登录成功，可以开始使用学生管理系统了！");
                TustStudentSystem.startTustStudentSystem();
            } else {
                System.out.println("用户名或密码不正确，登录失败！");
                if (i == 2) {
                    System.out.println("当前账号" + username + "被锁定，请联系黑马程序员客服：XXX-XXXXX");
                    return;
                } else {
                    System.out.println("用户名或密码错误，还剩下" + (2 - i) + "次机会");
                }
            }

        }
    }

    private static boolean checkUserInfo(ArrayList<User> userList, User userInfo) {

        for (int i = 0; i < userList.size(); i++) {
            User user = userList.get(i);
            if (user.getUsername().equals(userInfo.getUsername()) && user.getPassword().equals(userInfo.getPassword())) {
                return true;
            }
        }
        return false;
    }

    private static void register(ArrayList<User> userList) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入用户名：");
            String username = scanner.next();
            if (!checkUsername(username)) {
                System.out.println("用户名格式不满足要求，需要重新输入！");
                continue;
            }
            if (contains(userList, username)) {
                System.out.println("用户名已存在，需要重新输入！");
                continue;
            }
            String password;
            while (true) {
                System.out.println("请输入密码：");
                password = scanner.next();
                System.out.println("请输入确认密码：");
                String rePassword = scanner.next();

                if (password.equals(rePassword)) {
                    break;
                }

                System.out.println("两次密码不一致，请重新输入！");

            }

            String idCard;
            while (true) {
                System.out.println("请输入身份证号码：");
                idCard = scanner.next();
                if (checkIdCard(idCard)) {
                    break;
                }
                System.out.println("身份证号码格式有误，请重新输入！");
            }
            String phone;
            while (true) {
                System.out.println("请输入手机号码：");
                phone = scanner.next();
                if (checkPhone(phone)) {
                    break;
                }
                System.out.println("手机号码格式有误，请重新输入！");
            }

            User user = new User(username, password, idCard, phone);
            userList.add(user);
            System.out.println("注册成功！");

            printUserList(userList);
            break;
        }


    }

    private static void printUserList(ArrayList<User> userList) {
        for (int i = 0; i < userList.size(); i++) {
            User user = userList.get(i);
            System.out.println(user.getUsername() + ", " + user.getIdCard() + ", " + user.getPhone());
        }
    }

    private static boolean checkPhone(String phone) {
        if (phone.length() != 11) {
            return false;
        }
        if (phone.startsWith("0")) {
            return false;
        }
        for (int i = 0; i < phone.length(); i++) {
            char c = phone.charAt(i);
            if (!(c >= '0' && c <= '9')) {
                return false;
            }
        }
        return true;

    }

    private static boolean checkIdCard(String idCard) {
        if (idCard.length() != 18) {
            return false;
        }

        if (idCard.charAt(0) == '0') {
            return false;
        }

        for (int i = 0; i < idCard.length() - 1; i++) {
            char c = idCard.charAt(i);
            if (!(c >= '0' && c <= '9')) {
                return false;
            }
        }

        char endChar = idCard.charAt(17);

        if (!((endChar >= '0' && endChar <= '9') || (endChar == 'x') || (endChar == 'X'))) {
            return false;
        }

        return true;
    }

    private static boolean checkUsername(String username) {
        int len = username.length();
        if (len < 3 || len > 15) {
            return false;
        }

        for (int i = 0; i < username.length(); i++) {
            char c = username.charAt(i);
            if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))) {
                return false;
            }
        }

        int count = 0;
        for (int i = 0; i < username.length(); i++) {
            char c = username.charAt(i);
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                count++;
                break;
            }
        }
        return count > 0;
    }

    private static void forgotPassword(ArrayList<User> userList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scanner.next();
        if (!contains(userList, username)) {
            System.out.println("当前用户" + username + "未注册，请先注册");
            return;
        }

        System.out.println("请输入身份证号码：");
        String idCard = scanner.next();
        System.out.println("请输入手机号码：");
        String phone = scanner.next();

        int index = findIndex(userList, username);
        User user = userList.get(index);

        if (!user.getIdCard().equalsIgnoreCase(idCard) && user.getPhone().equals(phone)) {
            System.out.println("身份证号码或手机号码输入有误，不能修改密码");
            return;
        }

        String password;

        while (true) {
            System.out.println("请输入新的密码：");
            password = scanner.next();
            System.out.println("请再次输入新的密码：");
            String rePassword = scanner.next();
            if (password.equals(rePassword)) {
                break;
            }
            System.out.println("两次密码输入不一致，请重新输入！");
        }
        user.setPassword(password);
        System.out.println("密码修改成功！");

    }

    private static int findIndex(ArrayList<User> userList, String username) {
        for (int i = 0; i < userList.size(); i++) {
            User user = userList.get(i);
            if (user.getUsername().equals(username)) {
                return i;
            }
        }
        return -1;
    }

    private static boolean contains(ArrayList<User> userList, String username) {

        for (int i = 0; i < userList.size(); i++) {
            User user = userList.get(i);
            if (user.getUsername().equals(username)) {
                return true;
            }
        }

        return false;
    }

    private static String getCode() {
        ArrayList<Character> alphabet = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            alphabet.add((char) ('a' + i));
            alphabet.add((char) ('A' + i));
        }

        Random random = new Random();

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < 4; i++) {
            int randomIndex = random.nextInt(alphabet.size());
            sb = sb.append(alphabet.get(randomIndex));
        }

        int randomNumber = random.nextInt(10);

        sb = sb.append(randomNumber);

        String code = sb.toString();

        char[] chars = code.toCharArray();

        //随机交换数字位置
        int randomIndex = random.nextInt(chars.length);
        char temp = chars[randomIndex];
        chars[randomIndex] = chars[chars.length - 1];
        chars[chars.length - 1] = temp;

        code = new String(chars);

        return code;
    }

}
